package com.qiang.test06;

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class SysyemDemo {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //新建一个集合
        //Properties pp = new Properties();

        //新建反序列，读取文件中的账户名和密码

        //新建序列
        ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("day14\\demo01.txt"));
        Teacher teacher = new Teacher("dongzi", "1234");
        Teacher teacher2 = new Teacher("cuichang", "1234");
        oo.writeObject(teacher);
        oo.writeObject(teacher2);


        //输入学生信息
        ObjectOutputStream oo2 = new ObjectOutputStream(new FileOutputStream("day14\\stu.txt"));
        Student student = new Student("001", "猛哥", 18, "男");
        Student student2 = new Student("002", "龙哥", 18, "男");
        Student student3 = new Student("003", "老8", 18, "女");
        oo2.writeObject(student);
        oo2.writeObject(student2);
        oo2.writeObject(student3);
        oo.close();

        //读取学生信息
        ObjectInputStream oss = new ObjectInputStream(new FileInputStream("day14\\stu.txt"));
        Object oq = oss.readObject();
        Object oq2 = oss.readObject();
        Object oq3 = oss.readObject();
        Student stu = (Student) oq;
        Student stu2 = (Student) oq2;
        Student stu3 = (Student) oq3;
        ArrayList<Student> list = new ArrayList<>();
        list.add(stu);
        list.add(stu2);
        list.add(stu3);

        oss.close();


        //读取老师信息
        ObjectInputStream os = new ObjectInputStream(new FileInputStream("day14\\demo01.txt"));
        Object o = os.readObject();
        Object o1 = os.readObject();
        Teacher t = (Teacher) o;
        Teacher t2 = (Teacher) o1;
        os.close();


        Scanner sc = new Scanner(System.in);

        System.out.println("请输入您的账户名");
        String username = sc.next();

        System.out.println("请输入您的密码");
        String password = sc.next();
        while (true) {
            if ((username.equals(t.getUsername()) && (password.equals(t.getPassword())))) {
                System.out.println("登录成功");
                break;
            }
            if ((username.equals(t2.getUsername()) && (password.equals(t2.getPassword())))) {
                System.out.println("登录成功");
                break;
            } else {
                System.out.println("账户名或者密码错误，请重新输入");
                System.out.println("请输入您的账户名");
                username = sc.next();

                System.out.println("请输入您的密码");
                password = sc.next();
            }
        }

        while (true) {
            System.out.println("欢迎来到老师管理学生信息系统");
            System.out.println("1.查看学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.退出");
            System.out.println("请选择要操作的系统");
            int i = sc.nextInt();
            lo:
            while (true) {
                switch (i) {
                    case 1:
                        //findStduent();
                        //遍历集合
                        System.out.println("学生id" + "\t" + "\t" + "学生姓名" + "\t" + "\t" + "学生年龄" + "\t" + "\t" + "学生性别");
                        for (Student st : list) {
                            System.out.println(st.getId() + "\t" + "\t" + "\t" + st.getName() + "\t" + "\t" + "\t" + st.getAge() + "\t" + "\t" + "\t" + st.getXingbie());
                        }

                        break lo;
                    case 2:
                        //setStduent();
                        //判断id否存存在
                        loo:
                        while (true) {
                            System.out.println("请输入要修改的学生id");
                            String id = sc.next();
                            int index = -1;
                            for (int j = 0; j < list.size(); j++) {
                                if (list.size() != 0 && id.equals(list.get(j).getId())) {
                                    index = j;

                                    System.out.println("请输入学生姓名");
                                    String name = sc.next();

                                    System.out.println("请输入学生年龄");
                                    int age = sc.nextInt();

                                    System.out.println("请输入学生性别");
                                    String xingbie = sc.next();

                                    Student s = new Student();
                                    s.setId(id);
                                    s.setName(name);
                                    s.setAge(age);
                                    s.setXingbie(xingbie);
                                    list.set(index, s);

                                    //写入本地
                                    oo2.writeObject(list);
                                    System.out.println("修改成功");
                                    break lo;
                                }
                            }
                            System.out.println("id不存在，请重新输入");
                            //break;
                           /* for (Student st : list) {
                                if (st != null && id.equals(st.getId())) {
                                    System.out.println("请输入学生姓名");
                                    String name = sc.next();

                                    System.out.println("请输入学生年龄");
                                    int age = sc.nextInt();

                                    System.out.println("请输入学生性别");
                                    String xingbie = sc.next();

                                    Student s = new Student();
                                    s.setName(name);
                                    s.setAge(age);
                                    s.setXingbie(xingbie);

                                    //写入本地
                                    oo2.writeObject(s);
                                    System.out.println("修改成功");
                                    break lo;

                                } */
                        }
                    case 3:
                        //deleteStduent();
                        int index = -1;

                        while (true) {
                            System.out.println("请输入要删除的学生id");
                            String id = sc.next();
                            for (int j = 0; j < list.size(); j++) {
                                if (id.equals(list.get(j).getId())) {
                                    index = j;
                                    list.remove(index);

                                    //本地文件删除
                                    oo2.writeObject(list);

                                    System.out.println("删除成功");

                                    //本地文件也需要删除
                                    break lo;
                                }
                            }
                            /*for (Student st : list) {
                                if (st != null && id.equals(st.getId())) {
                                    list.remove(Integer.parseInt(id));

                                    //本地文件删除

                                    System.out.println("删除成功");
                                    break lo;
                                }
                            }*/
                            System.out.println("id不存在，请重新输入");
                        }
                    case 4:
                        System.exit(0);
                }
            }
        }

    }

    private static void findStduent() {

        System.out.println();

    }
}
